package doublepointer;

/**
 * Created by lh on 2019/7/22.
 * 题目描述：
 * 判断一个数是否为两个数的平方和。
 *
 * Example:
 * Input: 5
 * Output: True
 * Explanation: 1 * 1 + 2 * 2 = 5
 */
public class SquareSum {

    public static boolean judgeSquareSum(int num) {
        int i = 0, j = (int) Math.sqrt(num);
        while (i < j) {
            int sum = i * i + j * j;
            if (sum < num) {
                i++;
            } else if (sum > num) {
                j--;
            }else {
                System.out.println(i+"*"+i+"+"+j+"*"+j+"="+num);
                return true;
            }
        }
        return false;
    }
}
